﻿
using SYJ.Model.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using Dapper;
using SYJ.IDAL;
using SYJ.Factory;
using SYJ.Model;

namespace SYJ.MySqlDAL
{
    public class ContestPictureDAL : BaseDal<ContestPicture>, IContestPictureDAL 
    {

        /// <summary>
        /// 多表查询分页获取
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<ContestPicture> GetList(QueryModel query, out long count)
        {

            List<ContestPicture> list = null;
            using (Conn)
            {
                string sqlCount = string.Format("SELECT count(*)  FROM contest_picture a LEFT JOIN contest b on a.contest_id=b.id LEFT JOIN sys_user c on a.user_id=c.id where 1=1 {0} ", query.SqlWhere);
                string sql = string.Format(@"SELECT a.* ,b.title as contest_name,c.user_name  FROM contest_picture a LEFT JOIN contest b on a.contest_id=b.id LEFT JOIN sys_user c on a.user_id=c.id where 1=1 {2} order by {3} {4} LIMIT {0},{1};", query.PageSize * (query.PageIndex - 1), query.PageSize, query.SqlWhere, query.OrderName, query.SortOrder);
                if (query.Param == null)
                {
                    list = Conn.Query<ContestPicture>(sql).AsList();
                    count = Conn.ExecuteScalar<long>(sqlCount);
                }
                else
                {
                    list = Conn.Query<ContestPicture>(sql, query.Param).AsList();
                    count = Conn.ExecuteScalar<long>(sqlCount, query.Param);
                }
            }
            return list;
        }
   }
}
